package com.task.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.TaskRecord;
import com.entity.TaskRecordItem;
import com.interfaces.ItemDao;
import com.interfaces.TaskDao;
import com.interfaces.TaskRecordDao;
import com.interfaces.TaskRecordItemDao;
import com.task.entity.JsonTaskRecord;
import com.utils.JsonResult;

/**
 * @Title: TaskRecordService.java
 * @Package com.task.service
 * @author 孙梦楠
 * @date 2017年9月6日 上午8:33:02
 * @version V1.0.0
 */
@Service
public class TaskRecordService {

	@Autowired
	private TaskRecordDao taskrecorddao;

	@Autowired
	private TaskRecordItemDao taskrecorditemdao;

	@Autowired
	private ItemDao itemdao;
	
	@Autowired
	private TaskDao taskdao;

	// 给前端传查询明细
	@Transactional
	public JsonResult<List<JsonTaskRecord>> listRecord(Integer taskid, String time, String begintime, String endtime,
			Integer taskorg, String tasktype) {
		List<JsonTaskRecord> jsonTaskRecords = new ArrayList<JsonTaskRecord>();
		List<TaskRecord> records = taskrecorddao.listTaskRecord(taskid, time, begintime, endtime, taskorg, tasktype);
		for (TaskRecord taskRecord : records) {
			JsonTaskRecord jsonTaskRecord = new JsonTaskRecord();
			
			jsonTaskRecord.setId(taskRecord.getId());
			jsonTaskRecord.setTaskid(taskRecord.getTaskid());
			jsonTaskRecord.setTaskname(taskdao.findOne(taskRecord.getTaskid()).getTaskname());
			jsonTaskRecord.setTime(taskRecord.getTime());
			jsonTaskRecord.setBegintime(taskRecord.getBegintime());
			jsonTaskRecord.setEndtime(taskRecord.getEndtime());
			jsonTaskRecord.setTotalnum(taskRecord.getTotalnum());
			jsonTaskRecord.setSuccessnum(taskRecord.getSuccessnum());
			jsonTaskRecord.setSuccessrate(taskRecord.getSuccessrate());
			
			
			jsonTaskRecords.add(jsonTaskRecord);
		}

		return new JsonResult<List<JsonTaskRecord>>(623, "任务明细列表", jsonTaskRecords);
	}

	// 通过taskid查询所有的record
	@Transactional
	public JsonResult<List<TaskRecord>> listRecordByTaskid(Integer taskid) {

		List<TaskRecord> list = taskrecorddao.findByTaskid(taskid);

		return new JsonResult<List<TaskRecord>>(631, "通过任务ID查询记录", list);
	}

	// 保存执行结论到数据库
	@Transactional
	public boolean saveTaskRecord(Integer taskid, String terminaladdress,String time, String begintime, String endtime, String totalnum,
			String successnum, String successrate) {

		TaskRecord taskrecord = new TaskRecord();
		taskrecord.setTaskid(taskid);
		taskrecord.setTerminaladdress(terminaladdress);
		taskrecord.setTime(time);
		taskrecord.setBegintime(begintime);
		taskrecord.setEndtime(endtime);
		taskrecord.setTotalnum(totalnum);
		taskrecord.setSuccessnum(successnum);
		taskrecord.setSuccessrate(successrate);
		taskrecorddao.save(taskrecord);
		return true;
	}

	// 保存明细内容到数据库
	@Transactional
	public boolean saveTaskRecordItem(Integer taskid, String terminaladdress, String meteraddress, String time,
			String name, String code, String value, String updatetime) {

		TaskRecordItem taskrecorditem = new TaskRecordItem();
		taskrecorditem.setTaskid(taskid);
		taskrecorditem.setTerminaladdress(terminaladdress);
		taskrecorditem.setMeteraddress(meteraddress);
		taskrecorditem.setTime(time);
		taskrecorditem.setName(name);
		taskrecorditem.setCode(code);
		taskrecorditem.setValue(value);
		taskrecorditem.setUpdatetime(updatetime);
		taskrecorditemdao.save(taskrecorditem);
		return true;
	}

	// 保存多个明细内容到数据库
	@Transactional
	public boolean saveTaskRecordItem(List<TaskRecordItem> list) {
		Iterator<TaskRecordItem> Iterator = list.iterator();
		while (Iterator.hasNext()) {
			taskrecorditemdao.save(Iterator.next());
		}
		return true;
	}

	// 更新执行结论 传入 taskid 和begintime 修改endtime successnum successrate,totalnum
	@Transactional
	public boolean updateTaskRecordItem(Integer taskid, String time, String endtime, String successnum,
			String successrate,String totalnum,String terminaladdress) {

		taskrecorddao.updateTaskRecordItem(taskid, time, terminaladdress,endtime, successnum, successrate,totalnum);

		return true;
	}

	// 通过taskid查询所属的item
	@Transactional
	public boolean listItemByTaskid(Integer taskid) {
		itemdao.listItemByTaskid(taskid);

		return true;
	}

}
